package com.example.carconsumer.controller.dl.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.carinterface.service.dl.admin.BannerService;
import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(tags = "admin-Banner 管理")
@RestController
@RequestMapping("/admin/banner")
@CrossOrigin
public class BannerController {


    @Reference(check = false)
    private BannerService bannerService;

    @ApiOperation("banner列表")
    @GetMapping("/findAll")
    public ResultJson<List<Map<String, Object>>> findAll() {
        return bannerService.findAll();
    }

    @ApiOperation("根据banner的id删除")
    @GetMapping("/delById/{id}")
    public ResultJson<String> delById(@PathVariable("id") Integer id) {

        return bannerService.delById(id);
    }

    @ApiOperation("根据banner的id查询")
    @GetMapping("/findById/{id}")
    public ResultJson<Map<String, Object>> findById(@PathVariable("id") Integer id) {

        return bannerService.findById(id);
    }


    @PostMapping("/updateBanner")
    @ApiOperation("修改banner")
    public ResultJson<String> updateBanner(@ApiParam("id:banner id," +
            "imgUrl:封面地址，" +
            "url：作品连接，" +
            "name：标题") @RequestBody Map<String, Object> map) {

        return bannerService.updateBanner(map);
    }

    @ApiOperation("上传封面")
    @PostMapping("/uploadImgUrl")
    public ResultJson<String> uploadImgUrl(MultipartFile file, HttpServletRequest request) {

        System.out.println("开始上传");
        //上传路径
        String uploadFile = "";
        try {
             uploadFile = ResourceUtils.getURL("classpath:").getPath()+"static/upload/banner/";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        //获取文件名
        String fileName = file.getOriginalFilename();
        //唯一文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");
        fileName = uuid + "_" + fileName;

        File file0 = new File(uploadFile);
        file0.mkdirs();

        //保存文件
        try {
            file.transferTo(new File(uploadFile + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String  absolute = uploadFile+ fileName;
        String[] split = absolute.split("classes/static/");

        String relative = split[1];
        ResultJson<String> resultJson = new ResultJson<>();
        resultJson.setSuccess("上传成功",relative, 1L);

        return resultJson;
    }

    @ApiOperation("添加banner")
    @PostMapping("/addBanner")
    public ResultJson<Map<String, String>> addBanner(@ApiParam("imgUrl:封面地址，" +
            "url：作品连接，" +
            "name：标题") @RequestBody Map<String, Object> map) {

        return bannerService.addBanner(map);

    }
    @ApiOperation("移动")
    @PostMapping("/moveBanner")
    public ResultJson<String> moveBanner(@ApiParam("" +
            "修改后的：id:id," +
            "sort:排序号," +
            "to:1向下，-1向上") @RequestBody Map<String,Object> list){

        return bannerService.moveBanner(list);


    }


}
